package Model.Wall;

import java.io.IOException;
import java.net.HttpURLConnection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Model.Notification;

/**
 * Servlet implementation class postMessageServlet
 */
@WebServlet("/postMessageServlet")
public class PostMessageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//System.out.println("postMessageServlet");
		try {
			String message = request.getParameter("updateval");
//			String toIDString = request.getParameter("to");
//			int toID = Integer.parseInt(toIDString);
			
			HttpSession session = request.getSession();
			int fromID=-1;
			if(session.getAttribute("userID")!=null){
				fromID = (Integer) session.getAttribute("userID");
				//System.out.print("fromID not null:" + fromID);
			}
			
			int toID = (Integer) session.getAttribute("toID");
			//System.out.println(toID);
			
			//TODO: change fromID to ipt.getString("userID");
			//TODO: change toID
			PostTime postTime = new PostTime();
			int time = postTime.getCurrentTime();
			//System.out.println("postMessageServelet time:" + time);
			Post post = new Post(toID, fromID, time, message);
			int postID = post.getPostID();
			Notification.newNotification(toID, Notification.postNotificationMessage(postID), fromID);

		} catch(Exception e) {
			e.printStackTrace(System.err);
			response.setStatus(HttpURLConnection.HTTP_INTERNAL_ERROR);
		}
	}

}
